package com.howie.parnote.web.interact;

import com.howie.parnote.constant.RespMessageConstant;
import com.howie.parnote.exception.ApplicationProcessException;
import com.howie.parnote.module.SharerModule;
import com.howie.parnote.util.SharerUtil;
import com.howie.parnote.validator.bean.note.FormNoteBean;
import com.howie.parnote.web.ParnoteBaseController;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;

/**
 * <p>Title: parnote</p>
 * <p>
 * Description: parnote
 * </p>
 *
 * @author huangyan
 *         Created by HY on 2015/4/24 0024.
 * @version v1.0.0
 */
@Controller
public class NoteController extends ParnoteBaseController {

	/**
	 * FormNoteBean由CheckTagValidator验证,{@link com.howie.parnote.validator.consumer.CheckTagValidator#isValid}
	 *
	 * @param bean
	 * @param result
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "/action/share", method = RequestMethod.POST)
	public
	@ResponseBody
	String share(@Valid FormNoteBean bean, BindingResult result, HttpSession session) {
		if (result.hasErrors()) {
			return failResponse(RespMessageConstant.ILLEGAL_ARGUMENT);
		}
		SharerModule sharer = SharerUtil.getSharer(session);
		noteService.insertNote(bean, sharer);
		return successResponse();
	}

	@RequestMapping(value = "/feed")
	public
	@ResponseBody
	String feed(int noteId, String type, HttpSession session) {
		SharerModule sharer = SharerUtil.getSharer(session);
		try {
			noteService.insertNoteFeed(noteId, type, sharer);
			return successResponse();
		} catch (ApplicationProcessException e) {
			return failResponse();
		} catch (Exception e) {
			e.printStackTrace();
			return failResponse();
		}
	}

	@RequestMapping(value = "/comment", method = RequestMethod.POST)
	public
	@ResponseBody
	String comment(Integer noteId, String content, HttpSession session) {
		SharerModule sharer = SharerUtil.getSharer(session);
		try {
			noteService.insertComment(noteId, content, sharer);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return successResponse();
	}
}
